//
//  OpenClassVideoViewController.m
//  MIDI
//
//  Created by E.M.Game on 15/9/11.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "OpenClassVideoViewController.h"
#import "AppConfig.h"
#import "OpenClassVideoCell.h"
#import "OpenClassModel.h"
#import "UIViewController+Create.h"
#import "UIViewController+NavItem.h"
#import "APIAdress.h"
#import "QXNetWorkTool.h"
#import "OpenClassModel.h"
#import "MBProgressHUD+YC.h"
#import "UIImageView+WebCache.h"
#import "CommFunc.h"
#import "AboutMIDIAVWebViewController.h"
@interface OpenClassVideoViewController ()<UITableViewDataSource , UITableViewDelegate , QXNetWorkToolDelegate>
@property NSString *title;

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (assign , nonatomic) NSString * videoUrl;
@property (assign , nonatomic) BOOL isPlay;
@property (copy , nonatomic) NSString* lessonId;
@property (strong , nonatomic) OpenClassModel *data;
@property (strong , nonatomic) QXNetWorkTool *netTool;
@end

@implementation OpenClassVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.isPlay = NO;
    self.netTool = [[QXNetWorkTool alloc]init];
    self.netTool.QXNetWorkDelegate = self;
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
    [self reqData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void) configNavigationItem
{
    [self.tabBarController configNavigationBarTintColor:UIColorRGBA(0, 0, 0, 1)];
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"等级1课程";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];

    
}

-(void)setConfigData:(NSString*)mdata
{
    self.lessonId = mdata;
}

#pragma mark QxNetTool delegate

-(void)reqData
{
    if(self.lessonId == nil)
        return;
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:OpenClassDetailInterface andParameter:@[@{@"lession_id":self.lessonId}] tag:1];
    [MBProgressHUD showMessage:@"正在加载"];
}

-(void)GetSuccessMessage:(id)message andTag:(int)tag
{
    
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    if(tag == 1)
    {
        for(NSDictionary * dic in Response)
        {
            self.data = [OpenClassModel MyModelWithDict:dic];
            break;
        }
        [self.myTableView reloadData];
    }
    [MBProgressHUD hideHUD];
}

#pragma mark tableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat baseHeight = 587 - 255;
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 25 -25;
    CGFloat maxHeight = 999;
    CGSize maxSize = CGSizeMake(width, maxHeight);
    OpenClassModel *data = self.data;
    NSString *str = data.lesson_brief;
    UIFont *font = [UIFont fontWithName:@"Hiragino Sans GB" size:13];
    CGSize mySize = [CommFunc sizeWithString:str font:font max:maxSize];
    if(mySize.height > 255)
    {
        baseHeight = baseHeight + mySize.height;
    }else
    {
        baseHeight  = baseHeight + 255;
    }
    return baseHeight;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        OpenClassVideoCell * cell = [OpenClassVideoCell loadFromXib];
        [self configCell:indexPath.row cell:cell];
        return cell;
    }
    return nil;
}

#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


-(void)configCell : (NSInteger)row cell:(OpenClassVideoCell*)myCell
{
    if (self.data)
    {
        [myCell.playBtn addTarget:self action:@selector(clickPlayBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        if (self.data.lesson_covert != nil && ![self.data.lesson_covert isEqualToString:@""])
        {
            [myCell.videoImg sd_setImageWithURL:[NSURL URLWithString:self.data.lesson_covert]];
        }
        
        [myCell.videoNameLabel setText:self.data.lesson_name];
        [myCell.videoDetailLb setText:self.data.lesson_brief];
    }
    
    //[myCell.webView setScalesPageToFit:YES];
//    if(self.isPlay)  //如果是播放状态
//    {
//        myCell.playBtn.hidden = YES;
//        myCell.videoImg.hidden = YES;
//        
//        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
//        NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
//        
//        
//        NSString * urlStr = data[@"tudou"];
////        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
////        [myCell.webView loadRequest:request];
//        
//        //[myCell.webView loadHTMLString:urlStr baseURL:nil];
//        
//    }
}
//点击播放视频
-(void)clickPlayBtn:(UIButton*)sender
{
    self.isPlay = YES;
    [self.myTableView reloadData];
//    UIViewController * vc = [[UIViewController alloc]init];
//    UIWebView *webView = [[UIWebView alloc]initWithFrame:self.parentViewController.view.bounds];
////    NSURL *url = [NSURL URLWithString:self.data.lesson_url];
////    NSURLRequest *req = [NSURLRequest requestWithURL:url];
////    [webView loadRequest:req];
//    //TODO:wanglei 播放视频
//    [webView loadHTMLString:self.data.lesson_url baseURL:nil];
//    [vc.view addSubview:webView];
//    [self.parentViewController.navigationController pushViewController:vc animated:YES];
//    
//    [vc configNavigationBackAction:^{
//        [vc.navigationController popViewControllerAnimated:YES];
//    }];
    
    AboutMIDIAVWebViewController *vc = [AboutMIDIAVWebViewController create];
    
    vc.avUrl = self.data.lesson_url;
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
@end
